home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 3: The Continuation / 17-Bit_The_Continuation_Disc.iso / amigan / amigan 8 / hack.window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-27  |  9.8 KB  |  442 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
  2.  
  3. #include <exec/types.h>
  4. #include <exec/io.h>
  5. #include <intuition/intuition.h>
  6. #include <stdio.h>
  7. #include "hack.h"   /* for ROWNO and COLNO */
  8. #include "def.arrows.h"
  9. #include "def.menus.h"
  10.  
  11. #define XSIZE 8
  12. #define YSIZE 8
  13. #define BASEX 4
  14. #define BASEY 4   /* should be -4 */
  15.  
  16. #define ICON_REV       0
  17. #define GRAPHICS_REV   29
  18. #define INTUITION_REV  29
  19.  
  20. struct IntuitionBase *IntuitionBase;
  21. struct GfxBase *GfxBase;
  22. char *IconBase;
  23. int mousex, mousey;
  24.  
  25. #define BUFFERED 128
  26. char out_buffer[BUFFERED];
  27. int bufcnt;
  28. char *out_bufp;
  29.  
  30. struct Image URImage = { 0,0,22,10,2,(short *)&UPRIGHTdata,   0x3, 0, NULL };
  31. struct Image DRImage = { 0,0,22,10,2,(short *)&DOWNRIGHTdata, 0x3, 0, NULL };
  32. struct Image RImage  = { 0,0,22,10,2,(short *)&RIGHTdata,     0x3, 0, NULL };
  33. struct Image DImage  = { 0,0,22,10,2,(short *)&DOWNdata,      0x3, 0, NULL };
  34. struct Image UImage  = { 0,0,22,10,2,(short *)&UPdata,        0x3, 0, NULL };
  35. struct Image LImage  = { 0,0,22,10,2,(short *)&LEFTdata,      0x3, 0, NULL };
  36. struct Image DLImage = { 0,0,22,10,2,(short *)&DOWNLEFTdata,  0x3, 0, NULL };
  37. struct Image ULImage = { 0,0,22,10,2,(short *)&UPLEFTdata,    0x3, 0, NULL };
  38.  
  39. struct Gadget URGadget = { NULL,      436, 0, 22,10,
  40.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  41.             (APTR)&URImage, NULL, NULL, 0, NULL, 'u', NULL };
  42. struct Gadget DRGadget = { &URGadget, 568, 0, 22,10,
  43.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  44.             (APTR)&DRImage, NULL, NULL, 0, NULL, 'n', NULL };
  45. struct Gadget RGadget  = { &DRGadget, 490, 0, 22,10,
  46.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  47.             (APTR)&RImage, NULL, NULL, 0, NULL, 'l', NULL };
  48. struct Gadget DGadget  = { &RGadget,  544, 0, 22,10,
  49.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  50.             (APTR)&DImage, NULL, NULL, 0, NULL, 'j', NULL };
  51. struct Gadget UGadget  = { &DGadget,  412, 0, 22,10,
  52.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  53.             (APTR)&UImage, NULL, NULL, 0, NULL, 'k', NULL };
  54. struct Gadget LGadget  = { &UGadget,  466, 0, 22,10,
  55.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  56.             (APTR)&LImage, NULL, NULL, 0, NULL, 'h', NULL };
  57. struct Gadget DLGadget = { &LGadget,  520, 0, 22,10,
  58.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  59.             (APTR)&DLImage, NULL, NULL, 0, NULL, 'b', NULL };
  60. struct Gadget ULGadget = { &DLGadget, 388, 0, 22,10,
  61.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  62.             (APTR)&ULImage, NULL, NULL, 0, NULL, 'y', NULL };
  63.  
  64. struct TextAttr HackFont =
  65.     { "topaz.font", TOPAZ_EIGHTY,FS_NORMAL, FPF_ROMFONT};
  66.  
  67. struct Window *HackWindow;
  68.  
  69. struct NewWindow NewHackWindow = {
  70.   0,1,640,199, -1,-1,   /* left, top, width, height, detailpen, blockpen */
  71.   MOUSEBUTTONS | CLOSEWINDOW | RAWKEY | MENUPICK | GADGETDOWN,
  72.   WINDOWDEPTH | WINDOWCLOSE | ACTIVATE,
  73.   &ULGadget, NULL, "HACK V1.0.1 - Ported by John A. Toebes, VIII",
  74.   NULL, NULL, 640,200,640,200, WBENCHSCREEN };
  75.  
  76. struct IOStdReq consoleIO;
  77.  
  78. #define HO "\x9BH"
  79. #define CL "\x0C"
  80. #define CE "\x9BK"
  81. #define UP "\x0B"
  82. #define CM "\x9B%d;%dH"
  83. #define ND "\x09"
  84. #define XD "\x9BB"
  85. #define BC "\x08"
  86. #define SO "\x9B4m"
  87. #define SE "\x9B0m"
  88. #define BELL 7
  89. int myx, myy;
  90.  
  91. startup()
  92. {
  93. }
  94.  
  95. /* Cursor movements */
  96. extern xchar curx, cury;
  97.  
  98. curs(x,y)
  99. register int x,y;   /* not xchar: perhaps xchar is unsigned and
  100.             curx-x would be unsigned as well */
  101. {
  102.    if (y != cury || x != curx)
  103.     myprintf(CM, y, x);
  104.    curx = x;
  105.    cury = y;
  106. }
  107.  
  108. cl_end() {
  109.     myprintf(CE);
  110. }
  111.  
  112. clear_screen() {
  113.     myprintf(CL);
  114.     curx = cury = 1;
  115. }
  116.  
  117. home()
  118. {
  119.     myprintf(HO);
  120.     curx = cury = 1;
  121. }
  122.  
  123. standoutbeg()
  124. {
  125.     myprintf(SO);
  126. }
  127.  
  128. standoutend()
  129. {
  130.     myprintf(SE);
  131. }
  132.  
  133. backsp()
  134. {
  135.     myprintf(BC);
  136.     curx--;
  137. }
  138.  
  139. bell()
  140. {
  141.     myputchar(BELL);
  142. }
  143.  
  144. delay_output()
  145. {
  146.    /* delay 40 ms, 50 ticks/sec    */
  147.    Delay (2);
  148. }
  149.  
  150. initterm()
  151.    {
  152. #ifdef DEBUGIT
  153.     printf("intuition.library?\n");
  154.     fflush(stdout);
  155. #endif
  156.    if ( (IntuitionBase = (struct IntuitionBase *)
  157.     OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  158.     _exit(2);
  159.  
  160. #ifdef DEBUGIT
  161.     printf("graphics.library?\n");
  162.     fflush(stdout);
  163. #endif
  164.    if ( (GfxBase = (struct GfxBase *)
  165.     OpenLibrary("graphics.library",GRAPHICS_REV)) == NULL)
  166.     _exit(3);
  167.  
  168. #ifdef DEBUGIT
  169.     printf("icon.library?\n");
  170.     fflush(stdout);
  171. #endif
  172.    if ( (IconBase = (char *)
  173.     OpenLibrary("icon.library",ICON_REV)) == NULL)
  174.     _exit(4);
  175.  
  176. #ifdef DEBUGIT
  177.     printf("OpenWindow?\n");
  178.     fflush(stdout);
  179. #endif
  180.    if ( (HackWindow = (struct Window *)
  181.     OpenWindow(&NewHackWindow)) == NULL)
  182.     _exit(5);
  183.  
  184. #ifdef DEBUGIT
  185.     printf("menu strip?\n");
  186.     fflush(stdout);
  187. #endif
  188.    SetMenuStrip(HackWindow,&HackMenu);
  189.  
  190. #ifdef DEBUGIT
  191.     printf("console.device?\n");
  192.     fflush(stdout);
  193. #endif
  194.    consoleIO.io_Data = (APTR) HackWindow;
  195.    consoleIO.io_Length = sizeof(*HackWindow);
  196.    if (OpenDevice("console.device",0, &consoleIO, 0) != 0)
  197.     hackexit(6);
  198.  
  199.    SetFont(HackWindow->RPort,&HackFont);
  200.  
  201. #ifdef DEBUGIT
  202.     printf("doneinit\n");
  203.     fflush(stdout);
  204. #endif
  205.    bufcnt = 0;
  206.    out_bufp = out_buffer;
  207.    }
  208.  
  209. hackexit(code)
  210. int code;
  211.    {
  212.    CloseDevice(&consoleIO);
  213.    ClearMenuStrip(HackWindow);
  214.    CloseWindow(HackWindow);
  215.    CloseLibrary(IconBase);
  216.    CloseLibrary(GfxBase);
  217.    CloseLibrary(IntuitionBase);
  218.    _exit(code);
  219.    }
  220.  
  221. myfflush()
  222.    {
  223.    if (bufcnt)
  224.     {
  225.     consoleIO.io_Command = CMD_WRITE;
  226.     consoleIO.io_Data = (APTR)out_buffer;
  227.     consoleIO.io_Length = bufcnt;
  228.     DoIO(&consoleIO);
  229.     }
  230.    bufcnt = 0;
  231.    }
  232.  
  233. myputchar(c)
  234. char c;
  235.    {
  236.     if (bufcnt == BUFFERED)
  237.         myfflush();
  238.  
  239.     out_buffer[bufcnt++] = c;
  240.    }
  241.  
  242. myputs(str)
  243. char *str;
  244.    {
  245.     int len, tocopy;
  246.  
  247.     len = strlen(str);
  248.  
  249.     if (len >= BUFFERED)
  250.         {
  251.         myfflush();
  252.         consoleIO.io_Command = CMD_WRITE;
  253.         consoleIO.io_Data = (APTR)str;
  254.         consoleIO.io_Length = len;
  255.         DoIO(&consoleIO);
  256.         }
  257.     else
  258.         {
  259.         if (bufcnt+len >= BUFFERED) /* is there room */
  260.             {
  261.             tocopy = BUFFERED - bufcnt;
  262.             movmem(str, &out_buffer[bufcnt], tocopy);
  263.             bufcnt += tocopy;
  264.             len -= tocopy;
  265.             str += tocopy;
  266.             myfflush();
  267.             }
  268.         if (len)
  269.             {
  270.             /* just move it in */
  271.             movmem(str, &out_buffer[bufcnt], len);
  272.             bufcnt += len;
  273.             }
  274.         }
  275.     myputchar('\n');
  276.    }
  277.  
  278. /*VARARGS1*/
  279. myprintf(str,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  280. char *str,*a1,*a2,*a3,*a4,*a5,*a6,*a7,*a8,*a9;
  281.    {
  282.     char buf[BUFFERED], *bptr;
  283.     int len, tocopy;
  284.  
  285.     bptr = &buf;
  286.     len = (int)sprintf(bptr,str,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  287.     if (bufcnt+len >= BUFFERED) /* is there room */
  288.         {
  289.         tocopy = BUFFERED - bufcnt;
  290.         movmem(bptr, &out_buffer[bufcnt], tocopy);
  291.         bufcnt += tocopy;
  292.         len -= tocopy;
  293.         bptr += tocopy;
  294.         myfflush();
  295.         }
  296.     if (len)
  297.         {
  298.         /* just move it in */
  299.         movmem(bptr, &out_buffer[bufcnt], len);
  300.         bufcnt += len;
  301.         }
  302.    }
  303.  
  304. inchar()
  305.    {
  306.    struct IntuiMessage *Message, *GetMsg();
  307.    int c;
  308.    USHORT thismenu, menusel;
  309.    struct Gadget *gadget;
  310.    struct MenuItem *item, *ItemAddress();
  311.    c = 0;
  312.    while(!c)
  313.       {
  314.       while( (Message = GetMsg(HackWindow->UserPort)) == NULL)
  315.          Wait( 1 << HackWindow->UserPort->mp_SigBit );
  316.  
  317.       switch(Message->Class)
  318.     {
  319.     case MENUPICK:
  320.         menusel = thismenu = Message->Code;
  321.         while(thismenu != MENUNULL)
  322.            {
  323.            menusel = thismenu;
  324.            item = ItemAddress(&HackMenu, thismenu);
  325.            thismenu = item->NextSelect;
  326.            }
  327.         if (menusel != MENUNULL)
  328.            c = menukey[MENUNUM(menusel)][ITEMNUM(menusel)];
  329.         break;
  330.     case MOUSEBUTTONS:
  331.         mousex = ( (Message->MouseX) + BASEX ) / XSIZE;
  332.          mousey = ( (Message->MouseY) - BASEY ) / YSIZE;
  333.         if (mousex > 0 && mousey > 0 &&
  334.             mousex <= COLNO && mousey <= ROWNO )
  335.             {
  336.             if (Message->Code == SELECTDOWN)
  337.                 c = MDOWN;
  338.             else if (Message->Code == SELECTUP)
  339.                 c = MUP;
  340.             }
  341.         break;
  342.     case CLOSEWINDOW:
  343.         c = 'Q';
  344.         break;
  345.     case GADGETDOWN:
  346.         gadget = (struct Gadget *)Message->IAddress;
  347.         c = gadget->GadgetID;
  348.         break;
  349.     case RAWKEY:
  350.         c = cnvrtkey(Message->Code,Message->Qualifier);
  351.         break;
  352.     default:
  353.         c = 'Q';
  354.         break;
  355.     }
  356.       ReplyMsg(Message);
  357.       }
  358.    return(c);
  359.    }
  360.  
  361. #define NORMAL 0
  362. #define SHIFTED 1
  363. #define CONTROL 2
  364. #define ALTED 3
  365. short lookup[4][96] =
  366. {
  367. /* unshifted table */
  368.     '`',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
  369.     '8',    '9',    '0',    '-',    '=',    '\\',    0,    '0',
  370.     'q',    'w',    'e',    'r',    't',    'y',    'u',    'i',
  371.     'o',    'p',    '[',    ']',    0,    'b',    'j',    'n',
  372.     'a',    's',    'd',    'f',    'g',    'h',    'j',    'k',
  373.     'l',    ';',    '\'',    0,    0,    'h',    '.',    'l',
  374.     0,    'z',    'x',    'c',    'v',    'b',    'n',    'm',
  375.     ',',    '.',    '/',    0,    '.',    'y',    'k',    'u',
  376.     ' ',    8,    'i',    '\n',    '\n',    022,    8,    0,
  377.     0,    0,    '-',    0,    'k',    'j',    'l',    'h',
  378.     0,    0,    0,    0,    0,    0,    0,    0,
  379.     0,    0,    0,    0,    0,    0,    0,    '?',
  380.  
  381. /* shifted table */
  382.     '~',    '!',    '@',    '#',    '$',    '%',    '^',    '&',
  383.     '*',    '(',    ')',    '_',    '+',    '|',    0,    '0',
  384.     'Q',    'W',    'E',    'R',    'T',    'Y',    'U',    'I',
  385.     'O',    'P',    '{',    '}',    0,    'B',    'J',    'N',
  386.     'A',    'S',    'D',    'F',    'G',    'H',    'J',    'K',
  387.     'L',    ':',    '"',    0,    0,    'H',    '.',    'L',
  388.     0,    'Z',    'X',    'C',    'V',    'B',    'N',    'M',
  389.     '<',    '>',    '?',    0,    '.',    'Y',    'K',    'U',
  390.     ' ',    'H',    'I',    '\N',    '\N',    022,    'H',    0,
  391.     0,    0,    '-',    0,    'K',    'J',    'L',    'H',
  392.     0,    0,    0,    0,    0,    0,    0,    0,
  393.     0,    0,    0,    0,    0,    0,    0,    '?',
  394.  
  395. /* controlled table */
  396.     0,    0,    0,    0,    0,    0,    0,    0,
  397.     0,    0,    0,    0,    0,    'Q',    0,    0,
  398.     0,    0,    0,    022,    024,    0,    0,    0,
  399.     0,    020,    0,    0,    0,    0,    0,    0,
  400.     0,    0,    0,    0,    0,    0,    0,    0,
  401.     0,    0,    0,    0,    0,    0,    0,    0,
  402.     0,    0,    0,    0,    0,    0,    0,    0,
  403.     0,    0,    0,    0,    0,    0,    0,    0,
  404.     0,    0,    0,    0,    0,    0,    0,    0,
  405.     0,    0,    0,    0,    0,    0,    0,    0,
  406.     0,    0,    0,    0,    0,    0,    0,    0,
  407.     0,    0,    0,    0,    0,    0,    0,    '?',
  408.  
  409. /* alted table */
  410.     '`',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
  411.     '8',    '9',    '0',    '-',    '=',    '\\',    0,    '0',
  412.     'q',    'w',    'e',    'r',    't',    'y',    'u',    'i',
  413.     'o',    'p',    '[',    ']',    0,    'b',    'j',    'n',
  414.     'a',    's',    'd',    'f',    'g',    'h',    'j',    'k',
  415.     'l',    ';',    '\'',    0,    0,    'h',    '.',    'l',
  416.     0,    'z',    'x',    'c',    'v',    'b',    'n',    'm',
  417.     ',',    '.',    '?',    0,    '.',    'y',    'k',    'u',
  418.     ' ',    'h',    'i',    '\n',    '\n',    022,    'h',    0,
  419.     0,    0,    '-',    0,    'k',    'j',    'l',    'h',
  420.     0,    0,    0,    0,    0,    0,    0,    0,
  421.     0,    0,    0,    0,    0,    0,    0,    '?'
  422. };
  423.  
  424. int cnvrtkey(code, qual )
  425. USHORT code, qual;
  426.    {
  427.    int table;
  428.  
  429.    if (code > 0x5f)
  430.       return(0);
  431.  
  432.    if (qual & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  433.       table = SHIFTED;
  434.    else if (qual & (IEQUALIFIER_LALT | IEQUALIFIER_RALT))
  435.        table = ALTED;
  436.    else if (qual & (IEQUALIFIER_CONTROL))
  437.       table = CONTROL;
  438.    else
  439.       table = NORMAL;
  440.    return((int)lookup[table][code]);
  441.    }
  442.